<template>
    <div class="execution">
        <basic-container>
            <avue-crud ref="crud"
                       :page.sync="page"
                       :data="tableData"
                       :permission="permissionList"
                       :table-loading="tableLoading"
                       :option="tableOption"
                       @on-load="getList"
                       @search-change="searchChange"
                       @refresh-change="refreshChange"
                       @size-change="sizeChange"
                       @current-change="currentChange"
                       @row-update="handleUpdate"
                       @row-del="rowDel">
                <template slot="menu"  slot-scope="scope">
                    <el-button size="small"  type="text" icon="el-icon-edit" v-if="scope.row.smsStatus=='9'||scope.row.smsStatus=='2'" @click="againSend(scope.row)">重新发送</el-button>
                    <el-button size="small"  type="text" icon="el-icon-edit" v-if="scope.row.smsStatus=='9'||scope.row.smsStatus=='0'" @click="quxiao(scope.row)">取消</el-button>
                    <el-button size="small"  type="text" icon="el-icon-view" @click="chakanxiangqing(scope.row)">查看</el-button>
                </template>
            </avue-crud>
            <SmsView ref="SmsView"></SmsView>
        </basic-container>
    </div>
</template>

<script>
    import {fetchList, addObj, putObj, delObj} from '~/admin/api/note'
    import {tableOption} from '~/admin/const/note'
    import {mapGetters} from 'vuex'
    import SmsView from "./sms-view";

    export default {
        name: 'sms',
        components: {SmsView},
        data() {
            return {
                searchForm: {},
                tableData: [
                ],
                page: {
                    total: 0, // 总页数
                    currentPage: 1, // 当前页数
                    pageSize: 20 // 每页显示多少条
                },
                tableLoading: false,
                tableOption: tableOption,
                onceJudge: true,
                shuzhi: {
                    'receiveName': '',
                    'receiveMobile':'',
                    'smsContent':'',
                    'sendTime':'',
                },
            }
        },
        computed: {
            ...mapGetters(['permissions']),
            permissionList() {
                return {
                    addBtn: this.vaildData(this.permissions.sms_edit, false),
                    delBtn: this.vaildData(this.permissions.sms_del, false),
                    editBtn: this.vaildData(this.permissions.sms_edit, false)
                };
            }
        },
        mounted(){
            document.addEventListener('click',this.handleSaveClick,true)
            this.$on("hooks:beforeDestroy",() => {
                document.removeEventListener("click", this.handleSaveClick, true);
            })
        },
        methods: {
            handleSaveClick(ev){
                var ev = ev || window.event;
                var target = ev.target || ev.srcElement;
                if(target.className === 'el-button el-button--primary el-button--small' && target.innerText == '保 存') {
                    if(this.onceJudge==true) {

                        this.shuzhi.receiveName=this.$refs.crud.getPropRef('receiveName').value
                        this.shuzhi.receiveMobile=this.$refs.crud.getPropRef('receiveMobile').value
                        this.shuzhi.sendTime=this.$refs.crud.getPropRef('sendTime').value
                        this.shuzhi.smsContent=this.$refs.crud.getPropRef('smsContent').value

                        let nameNumber=0
                        let phoneNumber=0
                        let count=0
                        for(var i=0;i<this.shuzhi.receiveName.length-1;i++){
                            if(this.shuzhi.receiveName[i]!=','&&this.shuzhi.receiveName[i]!='，'){
                                if(count==0){
                                    count++
                                    nameNumber++
                                }
                            }else{
                                count=0
                            }
                        }

                        count=0
                        for(var i=0;i<this.shuzhi.receiveMobile.length-1;i++){
                            if(this.shuzhi.receiveMobile[i]!=','&&this.shuzhi.receiveMobile[i]!='，'){
                                if(count==0){
                                    count++
                                    phoneNumber++
                                }
                            }else{
                                count=0
                            }
                        }

                        if(nameNumber!=phoneNumber){

                            this.onceJudge && this.$confirm('您的接收人数量与接收人号码数量不一致, 是否继续?', '提示', {
                                confirmButtonText: '确定',
                                cancelButtonText: '取消',
                                type: 'warning'
                            }).then(() => {
                                this.onceJudge=false
                                addObj(this.shuzhi).then(data => {
                                    if (data.data.data.length != 0) {
                                        var k = "";
                                        for (var i = 0; i < data.data.data.length; i++) {
                                            k += data.data.data[i] + "\n";
                                        }
                                        alert("添加失败的短信有：\n" + k)
                                    } else {
                                        this.$message.success('添加成功')
                                        this.refreshChange
                                    }this.$refs.crud.$refs.dialogForm.closeDialog()
                                    done()
                                    this.getList(this.page)
                                }).catch(loading => {
                                    loading();
                                }).finally(() => {
                                    this.onceJudge = false
                                });
                            }).catch(() => {
                                this.$message({
                                    type: 'info',
                                    message: '已取消提交'
                                });
                                this.onceJudge = false
                            });
                        }else{
                            addObj(this.shuzhi).then(data => {
                                if (data.data.data.length != 0) {
                                    var k = "";
                                    for (var i = 0; i < data.data.data.length; i++) {
                                        k += data.data.data[i] + "\n";
                                    }
                                    alert("添加失败的短信有：\n" + k)
                                } else {
                                    this.$message.success('添加成功')
                                    this.refreshChange
                                }this.$refs.crud.$refs.dialogForm.closeDialog()
                                done()
                                this.getList(this.page)
                            }).catch(loading => {
                                loading();
                            }).finally(() => {
                                this.onceJudge = false
                            });
                        }
                    }
                    this.onceJudge=true
                    ev.stopPropagation();
                }
            },
            againSend(row){
                row.smsStatus='2'
                putObj(row).then(response=>{
                    this.$message.success('修改成功')
                    this.getList(this.page)
                }).catch(()=>{
                    this.$message.success('修改失败')
                })
            },
            quxiao(row){
                row.smsStatus='0'
                putObj(row).then(response=>{
                    this.$message.success('修改成功')
                    this.getList(this.page)
                }).catch(()=>{
                    this.$message.success('修改失败')
                })
            },
            chakanxiangqing(row){
                this.$refs.SmsView.show(row.smsId);
            },
            getList(page, params) {
                this.tableLoading = true
                fetchList(Object.assign({
                    current: page.currentPage,
                    size: page.pageSize
                }, params, this.searchForm )).then(response => {
                    this.tableData = response.data.data.records
                    this.page.total = response.data.data.total
                    this.tableLoading = false
                }).catch(() => {
                    this.tableLoading=false
                })
            },
            rowDel: function (row, index) {
                this.$confirm('是否确认删除?' , '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(function () {
                    return delObj(row.smsId)
                }).then(data => {
                    this.$message.success('删除成功')
                    this.getList(this.page)
                })
            },
            handleUpdate: function (row, index, done,loading) {
                putObj(row).then(data => {
                    this.$message.success('修改成功')
                    done()
                    this.getList(this.page)
                }).catch(() => {
                    loading();
                });
            },
            handleSave: function (row, done,loading) {
                /*    var phone=this.$refs.crud.getPropRef('receiveMobile').value
                    var name=this.$refs.crud.getPropRef('receiveName').value
                    var phonenumber=0;
                    var namecount=0;
                    var phoneshu=0;
                    for(var i=0;i<phone.length;i++){
                        phoneshu++;
                        // console.log(phoneshu)
                        if(phone[i]==','){
                            phonenumber++;
                            phoneshu=0;
                        }
                    }
                    if(phoneshu==11){
                        phonenumber++;
                    }
                    for(var i=0;i<name.length;i++){
                        if(name[0]==''){
                            continue;
                        }
                        if(name[i]==','){
                            namecount++;
                        }
                    }
                    if(name[name.length-1]!=','){
                        namecount++;
                    }*/
                addObj(row).then(data => {
                    if(data.data.data.length!=0){
                        var k="";
                        for(var i=0;i<data.data.data.length;i++){
                            k+=data.data.data[i]+"\n";
                        }
                        alert("添加失败的短信有：\n"+k)
                    }else{
                        this.$message.success('添加成功')
                    }
                    done()
                    this.getList(this.page)
                }).catch(() => {
                    loading();
                });
            },
            sizeChange(pageSize){
                this.page.pageSize = pageSize
            },
            currentChange(current){
                this.page.currentPage = current
            },
            searchChange(form, done) {
                this.searchForm = form
                this.page.currentPage = 1
                this.getList(this.page, form)
                done()
            },
            refreshChange() {
                this.getList(this.page)
            },
        }
    }
</script>
<style lang="scss" scoped>
    .avue-dialog .avue-form__group .el-form-item__content{
        .el-button {
            i,span{
                pointer-events: none;
            }
        }
    }
</style>
